$j(document).ready(function () {
	//验证
	applyValidate($("lowestShopPoint"),"integer");
	applyValidate($("highestShopPoint"),"integer");
	
});

$j.fn.extend({
	loading2:function(){
		return this.each(function(){
			$j(this).html('<td colspan="9"><div class="loading_box"><div class="progress_bar"><img src="'+__ctxPath+'/images/icon/loadingBig.gif"/>loading...</div></div></td>');			
		});
	}
});

//显示操作按钮
function displayListButtons(status){

}

//显示 隐藏全部
function toggleScreenAll(linkDOM){
	if($j(linkDOM).hasClass("expand")){
		$j(linkDOM).removeClass("expand").addClass("shrink");
		$j('span.toggleScreen').each(function(){
			var jTrThis = $j(this).parents('tr:first');
			var jTrFull = jTrThis.next();
			jTrFull.show();
			$j(this).removeClass("expand").addClass("shrink");
		});
	
	}else{
		$j(linkDOM).removeClass("shrink").addClass("expand");
		$j('span.toggleScreen').each(function(){
			var jTrThis = $j(this).parents('tr:first');
			var jTrFull = jTrThis.next();
			jTrFull.hide();
			$j(this).removeClass("shrink").addClass("expand");
		});
	}
}	

//显示 隐藏详细
function toggleScreen(linkDOM){
	var jTrThis = $j(linkDOM).parents('tr:first');
	var jTrFull = jTrThis.next();
	if(jTrFull.is(":hidden")){
		jTrFull.show();
		$j(linkDOM).removeClass("expand").addClass("shrink");
	}else{
		jTrFull.hide();
		$j(linkDOM).removeClass("shrink").addClass("expand");
	}
	
}

//创建客户
function createCustomer(){
	var url = __ctxPath+"/customer/customer/ajaxPage.html?doAction=add&direct=false";
	showCustomerForm(url);
}


//编辑客户
function editCustomer(customerId){
	var url = __ctxPath+"/customer/customer/ajaxPage.html?doAction=edit&direct=false&customerId="+customerId;
	showCustomerForm(url);
}

//在本页以ajaxPage方式打开customerForm 
function showCustomerForm(url,param){
	$j('#mainDiv').hide();
	$j('#formDiv').show().loading().load(url,param);
}

//删除多个
function deleteCustomers(){
	var paraData=$j("#customerListForm :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		var itemNames = fnGetSelectedItemNames();
		if(confirm( __FMT.common_message_confirmDeleteThis+ " " + itemNames+"?")){
			$j.post(__ctxPath + "/customer/customer/blank.html?doAction=multiDelete", paraData, deleteCustomersCallback,"json");
		}
	}else{
		alert(__FMT.customerList_selectCustomer);
	}
}
//删除后的回调函数
function deleteCustomersCallback(data){
	sysMsg(data.msg);
	refreshCustomerList();
}

//激活多个
function activeCustomers(){
	var paraData=$j("#customerListForm :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		var itemNames = fnGetSelectedItemNames();
		if(confirm( __FMT.common_message_confirmActiveThis+ " " + itemNames+"?")){
			$j.post(__ctxPath + "/customer/customer/blank.html?doAction=multiActive", paraData, activeCustomersCallback,"json");
		}
	}else{
		alert(__FMT.customerList_selectCustomer);
	}
}
//激活后的回调函数
function activeCustomersCallback(data){
	sysMsg(data.msg);
	refreshCustomerList();
}

//禁用多个
function forbidCustomers(){
	var paraData=$j("#customerListForm :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		var itemNames = fnGetSelectedItemNames();
		if(confirm( __FMT.common_message_confirmForbidThis+ " " + itemNames+"?")){
			$j.post(__ctxPath + "/customer/customer/blank.html?doAction=multiForbid", paraData, forbidCustomersCallback,"json");
		}
	}else{
		alert(__FMT.customerList_selectCustomer);
	}
}
//禁用后的回调函数
function forbidCustomersCallback(data){
	sysMsg(data.msg);
	refreshCustomerList();
}

//刷新列表
function refreshCustomerList(){
	searchCustomers(false);
}
//分页
function fnOnGoToPage(){
	refreshCustomerList();
}

//对已有结果进行排序,ascValue表示升序时对应的值,descValue表示降序时对应的值
function sortField(linkDOM,ascValue,descValue){
	var jThDOM = $j(linkDOM).parent('th');
	if(jThDOM.hasClass("order1")){
		$j('#storageOrderby').val(descValue);
	}else{
		$j('#storageOrderby').val(ascValue);
	}
	searchCustomers(false);
}

//搜索客户 isNew标示是否新的搜索,param标示是否自定义参数搜索
function searchCustomers(isNew,param){
	var postData;
	var url;
	if(isNew){
		if(null==param){
			if(validateSearchCriteria()){
				//有搜索结果搜索
			    postData = $j('#customerSearchCriteria :input').serializeArray();
			    url=__ctxPath+"/customer/customer/blank.html?doAction=search&btnSearch=1";
				$j("#customerResults").loading().load(url,postData);
		    }else{
		    	return;
		    }
		}else{
			//新的搜索。全部，上架等
			if(param.membershipId  != undefined){
				$j('#membershipId').val(""+param.membershipId);
			}
			//有搜索结果搜索
		    postData = $j('#customerResults :input').serializeArray();
		    url=__ctxPath+"/customer/customer/blank.html?doAction=search&btnSearch=1";
			$j("#customerResults").loading().load(url,postData);  
		}    
	}else{
		postData = $j('#customerResults :input').serializeArray();
		url=__ctxPath+"/customer/customer/blank.html?&doAction=search";
		$j("#customerResults").loading().load(url,postData);  
	} 
	
}



//验证函数
function validateSearchCriteria(){
	if(!validateForm($j('#customerSearchCriteria').get(0))){
    	return false;
   	}
   	return true;
}

//重设搜索条件
function resetSearchCriteria(){
	fnResetForm('customerSearchCriteria');
	$j('#customerSearchCriteria input[name="regionId"]').val("");//重置注册地区
	$j('#orderSearchCriteria select[name="regionName"]').each(function(){
		if($j(this).prevAll('select[name="regionName"]').length>=1){
			$j(this).remove();
		}
		
	});
}





//群发邮件初始化
function openSendEmailDialog(){
	var paraData=$j("#customerListForm :input[name='multiIds']:checked");
	if(paraData.length>0){
		$j('#receivers,#mailTitle,#content').val("");
		var receivers = "";
		paraData.each(function(){
			if($j(this).attr("email")!=""){
				receivers+=$j(this).attr("email")+";";
			}
		});
		receivers = receivers.substring(0,receivers.length-1);
		$j('#receivers').val(receivers);
		removeValidateMrak();
		dlgsendEmailDlg_show();
	}else{
		alert(__FMT.customerList_selectCustomer);
	}
}

//群发邮件
function sendEmail(){
	applyValidate($('receivers'),"required");
	applyValidate($('content'),"required");
	if(validateForm($('emailForm'))){
		var postData = $j('#emailForm :input').serializeArray();
		$j.post(__ctxPath + "/customer/customer/blank.html?doAction=sendMail", postData, sendEmailCallback,"json");
		closeSendEmailDialog()
	}else{
		jAlert(__vaMsg.notPass);
	}
	
	removeValidate($('receivers'));
	removeValidate($('content'));
}

//群发邮件
function sendEmailCallback(data){
	if(data.error){
		sysMsg(data.error,true);
	}else{
		sysMsg(data.msg);
	}	
}

//群发邮件窗口关闭
function closeSendEmailDialog(){
	dlgsendEmailDlg_close();
}
//清除验证后的痕迹
function removeValidateMrak(){
	$j('.x-form-invalid').removeClass("x-form-invalid");
	$j('.x-form-invalid-img').remove();
}

//保存重设的密码
function saveResetPassword(){
   	var form=$j("#passwordModel");
   	if(validateForm(form.get(0))){
   		var paramData=form.serializeArray();
   		function afterPost(data){
   			if (data.error == null) {
   				setTimeout("alert(__FMT.passwordReset_success)", 0);
					dlgresetPasswordDialog_close();
   			} else {
   				jAlert(data.error);
   			}
   		}
   		$j.post(__ctxPath+"/customer/customer/blank.html?doAction=saveResetPassword",paramData,afterPost,"json");
   	}else{
		alert(__vaMsg.notPass);
	}
}

//群发手机消息初始化
function openSendSmsDialog(){
	var paraData=$j("#customerListForm :input[name='multiIds']:checked");
	if(paraData.length>0){
		$j('#smsReceivers,#smsContent').val("");
		var receivers = "";
		var customerName = "";
		var n = 1;
		paraData.each(function(){
			if($j(this).attr("phone")!=""){
				receivers += $j(this).attr("phone")+";";
				customerName += $j(this).attr("title")+";";
				if(n%4 == 0){
					customerName += "<br>";
				}
				n++;
			}
		});
		if(n>1){
			receivers = receivers.substring(0,receivers.length - 1);
			customerName = customerName.substring(0,customerName.length - 1);
			$j('#smsReceivers').val(receivers);
			$j('#sms_userName').html(customerName);
			removeValidateMrak();
			dlgsendSmsDlg_show();
		}else{
			alert(__FMT.sms_customer_notmobilePhone);
			return false;
		}
	}else{
		alert(__FMT.customerList_selectCustomer);
	}
}
//群发手机消息
function sendSms(){
	applyValidate($('smsContent'),"required,maxlength=400");
	if(validateForm($('smsForm'))){
		var postData = $j('#smsForm :input').serializeArray();
		$j.post(__ctxPath + "/customer/customer/blank.html?doAction=sendSms", postData, sendSmsCallback,"json");
		closeSendSmsDialog()
	}
	removeValidate($('smsContent'));
}

//群发手机消息callback
function sendSmsCallback(data){
	if(data.error){
		sysMsg(data.error,true);
	}else{
		sysMsg(data.msg);
	}	
}

//群发消息窗口关闭
function closeSendSmsDialog(){
	dlgsendSmsDlg_close();
}

//群发系统消息初始化
function openSendSystemMsgDialog(){
	var paraData=$j("#customerListForm :input[name='multiIds']:checked");
	if(paraData.length>0){
		$j('#systemMsgReceivers,#systemMsgTitle,#systemMsgContent').val("");
		var receivers = "";
		var customerName = "";
		var n = 1;
		paraData.each(function(){
			if($j(this).attr("customerId")!=""){
				receivers += $j(this).attr("customerId")+";";
				customerName += $j(this).attr("title")+";";
				if(n%4 == 0){
					customerName += "<br>";
				}
				n++;
			}
		});
		receivers = receivers.substring(0,receivers.length - 1);
		customerName = customerName.substring(0,customerName.length - 1);
		$j('#systemMsgReceivers').val(receivers);
		$j('#msg_userName').html(customerName);
		removeValidateMrak();
		dlgsendSystemMsgDlg_show();
	}else{
		alert(__FMT.customerList_selectCustomer);
	}
}
//群发消息
function sendSystemMsg(){
	applyValidate($('systemMsgContent'),"required");
	if(validateForm($('systemMsgForm'))){
		var postData = $j('#systemMsgForm :input').serializeArray();
		$j.post(__ctxPath + "/customer/customer/blank.html?doAction=sendSystemMsg", postData, sendSystemMsgCallback,"json");
		closeSendSystemMsgDialog()
	}else{
		jAlert(__vaMsg.notPass);
	}
	removeValidate($('systemMsgContent'));
}

//群发消息callback
function sendSystemMsgCallback(data){
	if(data.error){
		sysMsg(data.error,true);
	}else{
		sysMsg(data.msg);
	}	
}

//群发消息窗口关闭
function closeSendSystemMsgDialog(){
	dlgsendSystemMsgDlg_close();
}
